-module(lib_misc).
-export([for/3, odds_and_evens1/1, odds_and_evens2/1, ls/1]).

-include_lib("kernel/include/file.hrl").

for(Max, Max, F)	-> [F(Max)];
for(I, Max, F)		-> [F(I)|for(I+1, Max, F)].

odds_and_evens1(L) ->
	Odds = [X || X <- L, (X rem 2) =:= 1],
	Evens = [X || X <- L, (X rem 2) =:= 0],
	{Odds, Evens}.

odds_and_evens2(L) ->
	odds_and_evens_acc(L, [], []).
odds_and_evens_acc([H|T], Odds, Evens) ->
	case (H rem 2) of
		1 -> odds_and_evens_acc(T, [H|Odds], Evens);
		0 -> odds_and_evens_acc(T, Odds, [H|Evens])
	end;
odds_and_evens_acc([], Odds, Evens) ->
	{lists:reverse(Odds), lists:reverse(Evens)}.

file_size_and_type(File) ->
	case file:read_file_info(File) of
		{ok, Facts} ->
			{Facts#file_info.type, Facts#file_info.size};
		_ ->
			error
	end.
ls(Dir) ->
	{ok, L} = file:list_dir(Dir),
	lists:map(fun(I) -> {I, file_size_and_type(I)} end, lists:sort(L)).
